// pages/salesmans/promotersModify/promotersModify.js
var app = getApp();
var village_id = '';
var village_data = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    communityList: ['恒大雅苑1', '恒大雅苑2', '恒大雅苑3', '恒大雅苑4'],
    communityI: 0,
    username: '张三',
    phonenum: '13800138000',
    cardnum: '513010298709078691',
    idcard: '',
    source: '',
    address: '',
    id:'',
    // village_data:[],
    // village_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({
      id: id,
      source: options.source_type,
    });
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    village_id = '';
    village_data = [];
    this.getWaterCardAudit();
  },




  // 选择园区
  communityChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      communityI: e.detail.value
    })
  },

  // 确认
  formSubmit: function (e) {
    // console.log(e.detail.value)
    var xq = this.data.communityList[e.detail.value.community]
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/editWaterCardAudit',
      method: "POST",
      data: {
        id: that.data.id, //水卡ID
        full_name: e.detail.value.username,
        tel: e.detail.value.phonenum,
        IDcard: e.detail.value.idcard,
        village_id: village_data[that.data.communityI].id,
        address: e.detail.value.address,
        village_name: village_data[that.data.communityI].village_name,
      },
      success: function (res) {
        var flag = res.data;
        app.showToast(flag.message);
        if (flag.code > 0) {
          setTimeout(function(){
            wx.navigateTo({
              url: '../promotersApplyList/promotersApplyList',
            })
          },1000);
        }
      }
    });
  },
  getWaterCardAudit() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getWaterCardAudit',
      method: "POST",
      data: {
        id: that.data.id //水卡ID
      },
      success: function (res) {
        var flag = res.data;
        if (flag.code > 0) {
          village_id = flag.data.village_id;
          that.setData({
            username: flag.data.full_name,
            phonenum: flag.data.tel,
            cardnum: flag.data.card_number,
            idcard: flag.data.IDcard,
            address: flag.data.address,
            // village_id: flag.data.village_id
          });
          that.VillageList();
        }
      }
    });
  },
  VillageList(){
    var that =this;
    app.ajax({
      url: 'Partner/Village/VillageList',
      method: "POST",
      data: {
        row:999
      },
      success: function (res) {
        var flag = res.data;
        if (flag.code > 0) {
          var village = [];
          var current = 0;
          for(var i = 0;i<flag.data.data.length;i++){
            village.push(flag.data.data[i].village_name);
            if (flag.data.data[i].id == village_id){
              current = i;
            }
          }
          console.log(current)
          village_data = flag.data.data;
          that.setData({
            // village_data: flag.data.data,
            communityList: village,
            communityI: current
          });
        }
      }
    });
  },
})